<template>
  <div class="wrapper">
    <!-- header部分 -->
    <header>
      <p>用户注册</p>
    </header>
    <!-- 表单部分 -->
    <ul class="form-box">
      <li>
        <div class="title">
          用户名：
        </div>
        <div class="content">
          <!-- <input type="text" @blur="checkUserId" v-model="user.userName" placeholder="用户名"> -->
          <input type="text" v-model="user.userName" placeholder="用户名">
        </div>
      </li>
      <li>
        <div class="title">
          密码：
        </div>
        <div class="content">
          <input type="password" v-model="user.password" placeholder="密码">
        </div>
      </li>
      <li>
        <div class="title">
          确认密码：
        </div>
        <div class="content">
          <input type="password" v-model="confirmPassword" placeholder="确认密码">
        </div>
      </li>
      <li>
        <div class="title">
          身份：
        </div>
        <div class="content" style="font-size: 3vw;">
          <input type="radio" v-model="authority" value="USER" style="width:6vw;height: 3.2vw;">用户
          <input type="radio" v-model="authority" value="BUSINESS" style="width:6vw;height: 3.2vw;margin-left: 2vw;">商家
        </div>
      </li>
    </ul>
    <div class="button-login">
      <button @click="register">注册</button>
    </div>
    <!-- 底部导航 -->
    <footer class="bottom-nav">
      <div class="nav-item" :class="{ active: $route.path === '/index' }" @click="$router.push('/index')">
        <i class="fa fa-home"></i>
        <span>首页</span>
      </div>
      <div class="nav-item">
        <i class="fa fa-compass"></i>
        <span>商家</span>
      </div>
      <div class="nav-item" :class="{ active: $route.path === '/orderList' }" @click="$router.push('/orderList')">
        <i class="far fa-file-alt"></i>
        <span>订单</span>
      </div>
      <div class="nav-item" :class="{ active: $route.path === '/userProfile' }" @click="$router.push('/userProfile')">
        <i class="fa fa-user"></i>
        <span>我的</span>
      </div>
    </footer>
  </div>
</template>
<script>
export default {
  name: 'Register',
  data() {
    return {
      user: {
        userName: '',
        password: ''
      },
      confirmPassword: '',
      authority: ''
    }
  },
  methods: {
    checkUserId() {
      this.$axios.get('user', {
        "username": this.user.userName,
        "password": this.user.password
      }).then(response => {
        if (response.data == 1) {
          this.user.userId = '';
          alert('此用户名已存在！')
        }
      }).catch(error => {
        console.error(error);
      });
    },
    register() {
      if (this.user.userName == '') {
        alert('用户名不能为空！');
        return;
      }
      if (this.user.password == '') {
        alert('密码不能为空！');
        return;
      }
      if (this.user.password != this.confirmPassword) {
        alert('两次输入的密码不一致！');
        return;
      }
      //注册请求
      this.$axios.post('/users', {
        "username": this.user.userName,
        "authorities": [
          {
            "name": this.authority
          }
        ],
        "password": this.user.password,
      }, {
        headers: {
          'Content-Type': 'application/json'  // 明确设置请求头为JSON格式
        }
      }).then(response => {
        if (response.data.data != null) {
          alert('注册成功！');
          this.$router.go(-1);//回退
        } else {
          alert('注册失败！');
        }
      }).catch(error => {
        console.error(error);
      });
    }
  }
}
</script>
<style scoped>
/****************** 总容器 ******************/
.wrapper {
  width: 100%;
  height: 100%;
}

/****************** header部分 ******************/
.wrapper header {
  width: 100%;
  height: 12vw;
  background-color: #0097FF;
  color: #fff;
  font-size: 4.8vw;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
  display: flex;
  justify-content: center;
  align-items: center;
}

/****************** 表单部分 ******************/
.wrapper .form-box {
  width: 100%;
  margin-top: 12vw;
}

.wrapper .form-box li {
  box-sizing: border-box;
  padding: 4vw 3vw 0 3vw;
  display: flex;
  align-items: center;
}

.wrapper .form-box li .title {
  flex: 0 0 18vw;
  font-size: 3vw;
  font-weight: 700;
  color: #666;
}

.wrapper .form-box li .content {
  flex: 1;
}

.wrapper .form-box li .content input {
  border: none;
  outline: none;
  width: 100%;
  height: 4vw;
  font-size: 3vw;
}

.wrapper .button-login {
  width: 100%;
  box-sizing: border-box;
  padding: 4vw 3vw 0 3vw;
}

.wrapper .button-login button {
  width: 100%;
  height: 10vw;
  font-size: 3.8vw;
  font-weight: 700;
  color: #fff;
  background-color: #38CA73;
  border-radius: 4px;
  border: none;
  outline: none;
}

.wrapper .button-register {
  width: 100%;
  box-sizing: border-box;
  padding: 4vw 3vw 0 3vw;
}

.wrapper .button-register button {
  width: 100%;
  height: 10vw;
  font-size: 3.8vw;
  font-weight: 700;
  color: #666;
  background-color: #EEE;
  border-radius: 4px;
  border: none;
  outline: none;
  border: solid 1px #DDD;
}

/* 底部导航 */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 16vw;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-top: 1px solid #e0e0e0;
  display: flex;
  justify-content: space-around;
  align-items: center;
  z-index: 100;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #999;
  font-size: 3vw;
  cursor: pointer;
  transition: all 0.3s ease;
  padding: 2vw;
  border-radius: 10px;
}

.nav-item.active {
  color: #0097FF;
  transform: translateY(-3px);
}

.nav-item i {
  font-size: 5.5vw;
  margin-bottom: 1vw;
  transition: all 0.3s ease;
}

.nav-item.active i {
  transform: scale(1.2);
}

.nav-item span {
  font-size: 3vw;
  font-weight: 500;
}
</style>